/*
 * Game.java
 *
 * Created on May 7, 2006, 8:33 PM
 */

package poppitplayer.gui;

import javax.swing.*;
import simpoppit.gameboard.*;
import simpoppit.gui.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;

/**
 *
 * @author  Andrew
 */
public class Game extends javax.swing.JFrame {
    
    /** Creates new form Game */
    public Game() {
        this.startForm = new Start();
        initComponents();
    }
    
    /** Creates new form Game */
    public Game(Start aForm) {
        this.startForm = aForm;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanelGame = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemNewGame = new javax.swing.JMenuItem();
        jMenuItemLoadGame = new javax.swing.JMenuItem();
        jMenuItemSaveGame = new javax.swing.JMenuItem();
        jMenuItemRestartGame = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItemAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Poppit");
        setBackground(new java.awt.Color(255, 255, 255));
        setName("gameFrame");
        jPanelGame.setLayout(new java.awt.GridLayout(1, 0));

        jPanelGame.setBackground(new java.awt.Color(255, 255, 255));
        jPanelGame.setPreferredSize(new java.awt.Dimension(450, 450));

        jMenu1.setText("File");
        jMenu1.setName("File");
        jMenuItemNewGame.setText("New Game");
        jMenuItemNewGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewGameActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemNewGame);

        jMenuItemLoadGame.setText("Load Game");
        jMenuItemLoadGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLoadGameActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemLoadGame);

        jMenuItemSaveGame.setText("Save Game");
        jMenuItemSaveGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveGameActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemSaveGame);

        jMenuItemRestartGame.setText("Restart Game");
        jMenu1.add(jMenuItemRestartGame);

        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItemExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");
        jMenuItemAbout.setText("About");
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItemAbout);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelGame, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelGame, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemSaveGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveGameActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            //System.out.println("Loading file " + file.getAbsolutePath());
            startForm.aGame.saveGame(file.getAbsolutePath());
        }else{
            System.out.println("User cancelled save.");
        }
    }//GEN-LAST:event_jMenuItemSaveGameActionPerformed

    private void jMenuItemLoadGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadGameActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            //System.out.println("Loading file " + file.getAbsolutePath());
            startForm.aGame.loadGame(file.getAbsolutePath());
            this.loadGameIntoGUI();
            startForm.gameForm.setVisible(true);
        }else{
            System.out.println("User cancelled load.");
        }
    }//GEN-LAST:event_jMenuItemLoadGameActionPerformed

    private void jMenuItemNewGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewGameActionPerformed
        this.setVisible(false);
        startForm.gameType.setVisible(true);
    }//GEN-LAST:event_jMenuItemNewGameActionPerformed

    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed
        JOptionPane.showMessageDialog(this, "PoppitPlayer (C) 2006 Andrew W. Henry", "About PoppitPlayer", JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_jMenuItemAboutActionPerformed
    
    public void loadGameIntoGUI(){
        //System.out.println(startForm.aGame);
        this.jPanelGame.removeAll();
        this.jPanelGame.setLayout(new GridLayout(startForm.aGame.getGridSize().getY()+1, startForm.aGame.getGridSize().getX()+1));
        for (Coord t : startForm.aGame.getGridAsList()) {
            BalloonButton balloon = new BalloonButton(startForm.aGame, t);
                balloon.addMouseMotionListener(new MouseMotionHandler());
                balloon.addMouseListener(new MouseHandler());
            this.jPanelGame.add(balloon);
            startForm.aGame.addGameListener(balloon);
        }
        this.jPanelGame.repaint();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Game().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemLoadGame;
    private javax.swing.JMenuItem jMenuItemNewGame;
    private javax.swing.JMenuItem jMenuItemRestartGame;
    private javax.swing.JMenuItem jMenuItemSaveGame;
    public javax.swing.JPanel jPanelGame;
    // End of variables declaration//GEN-END:variables
    private Start startForm;
}
